package com.simple.datax.flow.service.impl;

import java.util.List;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.simple.datax.flow.dao.ibatis.FlowDao;
import com.simple.datax.flow.dao.ibatis.FlowPreProcessorDao;
import com.simple.datax.flow.dao.ibatis.FlowTransmissionDao;
import com.simple.datax.flow.dao.ibatis.model.Flow;
import com.simple.datax.flow.dao.ibatis.model.FlowPreProcessor;
import com.simple.datax.flow.dao.ibatis.model.FlowTransmission;
import com.simple.datax.flow.service.PreProcessorManager;

/** 
 * @Title: PreProcessorManagerImpl.java 
 * @Package com.simple.datax.flow.service.impl 
 * @Description: TODO(添加描述) 
 * @author tonney 
 * @date 2013-1-7 下午03:14:07 
 * @version V1.0 
 */
public class PreProcessorManagerImpl implements PreProcessorManager {
	public Logger log = LoggerFactory.getLogger(this.getClass());

	FlowPreProcessorDao flowPreProcessorDao;
	
	FlowTransmissionDao flowTransmissionDao;
	
	FlowDao flowDao;
	
	public void setFlowDao(FlowDao flowDao) {
		this.flowDao = flowDao;
	}

	public void setFlowPreProcessorDao(FlowPreProcessorDao flowPreProcessorDao) {
		this.flowPreProcessorDao = flowPreProcessorDao;
	}

	public void setFlowTransmissionDao(FlowTransmissionDao flowTransmissionDao) {
		this.flowTransmissionDao = flowTransmissionDao;
	}

	@Override
	public FlowPreProcessor getFlowPreProcessor(String ecode,String messageType,String activityNo,String flowId) {
		List<FlowPreProcessor> preProcessorList = null;
		FlowPreProcessor retPreProcessor = null;
		try {
			String flowTemplateId = null;
			Flow flow = flowDao.getFlowById(flowId);
			if(flow!=null){
				flowTemplateId = flow.getFlowTemplateId();
			}
			if(flowTemplateId!=null){
				preProcessorList = flowPreProcessorDao.getFlowPreProcessor(flowTemplateId);
				if(preProcessorList!=null){
					for(FlowPreProcessor flowPreProcessor:preProcessorList){
						if(flowPreProcessor.geteCode().equals(ecode)){
							if((messageType==null?true:messageType.equals(flowPreProcessor.getMessageType()))&&
									(activityNo==null?true:activityNo.equals(flowPreProcessor.getActivityNo()))){
								retPreProcessor =flowPreProcessor;
							}
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("PreProcessorManagerImpl::getFlowPreProcessor error=>"+e.toString());
		}
		return retPreProcessor;
	}

	@Override
	public FlowTransmission getTransmission(FlowTransmission transmission,String flowId) {
		List<FlowTransmission> transmissionList = null;
		FlowTransmission retTransmission = null;
//		String flowTemplateId = null;
		try{
//			Flow flow = flowDao.getFlowById(flowId);
//			if(flow!=null){
//				flowTemplateId = flow.getFlowTemplateId();
//			}
//			if(flowTemplateId!=null&&!flowTemplateId.equals("")){
			transmissionList = flowTransmissionDao.getTransmission(transmission.geteCode()); 
			if(transmissionList!=null){
				for(FlowTransmission flowTransmission:transmissionList){
					if(flowTransmission.geteNode()!=null&&flowTransmission.getBpNo()!=null){
						if(flowTransmission.geteNode().equals(transmission.geteNode())&&flowTransmission.getBpNo().equals(flowId)){
							retTransmission = flowTransmission;
							break;
						}
					}
				}
			}
//			}
		}catch(Exception e){
			log.error("PreProcessorManagerImpl::getTransmission error=>"+e.toString());			
		}
		return retTransmission;
	}
}
